package com.aos.erp.schools.rest.sys;

import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.schools.enums.student.IsDel;
import com.aos.erp.schools.model.registerschool.entity.RegisterSchool;
import com.aos.erp.schools.model.sys.Position;
import com.aos.erp.schools.model.sys.dto.PositionDto;
import com.aos.erp.schools.model.sys.vo.PositionVo;
import com.aos.erp.schools.service.sys.Positionservice;
import com.aos.erp.schools.service.sys.UserService;
import com.aos.erp.schools.util.UserUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: erp
 * @description:岗位
 * @author: tanzhiyu
 * @create: 2018-11-05 18:31
 **/
@Api(value = "岗位api", tags = {"岗位API"})
@RestController("RESTPositionController")
@RequestMapping("/api/position")
public class PositionController {
    @Resource
    private Positionservice positionservice;

    @ApiOperation(value = "新增岗位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depCode", value = "部门code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "code", value = "职位代码", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = "职位名称", required = false, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "remarks", value = "职位描述", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping("/addPosition")
    public ResultEntity addPosition(@ApiParam(value = "参数")  PositionVo positionVo,String currentUserId) throws Exception {
        if(StringUtils.isEmpty(currentUserId) ){
            throw new BizException("请重新登录");
        }
        positionVo.setOperatorId(Long.valueOf(currentUserId));
        positionservice.addBean(positionVo);
        return ResultEntity.success();
    }

    @ApiOperation(value = "修改岗位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "岗位id", required = true, dataType = "Long", paramType = "Query"),
            @ApiImplicitParam(name = "depCode", value = "部门code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "code", value = "职位代码", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = "职位名称", required = false, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "remarks", value = "职位描述", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping("/updatePosition")
    public ResultEntity updatePosition(@ApiParam(value = "参数") PositionVo positionVo,String currentUserId) throws Exception {
        if(StringUtils.isEmpty(currentUserId) ){
            throw new BizException("请重新登录");
        }
        positionVo.setOperatorId(Long.valueOf(currentUserId));
        positionservice.updateBean(positionVo);
        return ResultEntity.success();
    }

    @ApiOperation(value = "删除岗位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "岗位ID", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/delPosition")
    public ResultEntity delPosition(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return ResultEntity.failed("岗位ID不能为空", ResultEntity.CODE_NULL);
        }
        int i = positionservice.isDel(id);
        if (i > 0) {
            return ResultEntity.success("删除成功");
        } else {
            return ResultEntity.success("删除失败");
        }
    }

    @ApiOperation(value = "根据岗位id获取信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "岗位ID", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getPosition")
    public ResultEntity getPosition(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return ResultEntity.failed("岗位ID不能为空", ResultEntity.CODE_NULL);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        PositionDto positionDto = positionservice.selectByParams(map);
        return ResultEntity.success(positionDto);
    }

    @ApiOperation(value = "获取部门下的岗位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depCode", value = "部门组织code", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getPositionBydepCode")
    public ResultEntity getPositionBydepCode(String depCode,String currentUserId) throws Exception {
        if (StringUtils.isBlank(depCode)) {
            //return ResultEntity.failed("部门组织code不能为空", ResultEntity.CODE_NULL);
            //TODO 部门组织code
            depCode= UserUtil.getUserDepCode(currentUserId);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("depCode", depCode);
        map.put("isDel", IsDel.normal.getValue().toString());
        List<PositionDto> list = positionservice.findByParams(map);
        return ResultEntity.success(list);
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = " 每页显示条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "depCode", value = "部门组织code", required = false, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "code", value = "岗位代码", required = false, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = "岗位名称", required = false, dataType = "String", paramType = "Query")
    })
    public ResultEntity<IPage<PositionDto>> queryPageBean(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                          @RequestParam(name = "limit", defaultValue = "10") Integer limit,
                                                          @RequestParam(name = "depCode", required = false) String depCode,
                                                          @RequestParam(name = "code", required = false) String code,
                                                          @RequestParam(name = "name", required = false) String name,
                                                            String currentUserId)throws Exception {
        if (StringUtils.isBlank(depCode)) {
            //return ResultEntity.failed("部门组织code不能为空", ResultEntity.CODE_NULL);
            //TODO 部门组织code
            depCode= UserUtil.getUserDepCode(currentUserId);
        }
        return ResultEntity.success(positionservice.queryByPage(page, limit, depCode,code,name));
    }

}
